<template>
  <input
    class="myinput"
    :[proName]="{ success: isOk, error: !isOk }"
    type="text"
    @input="handerInput"
    @blur="handerBlur"
  />
</template>

<script>
export default {
  data() {
    return {
      isOk: true,
      proName: "",
    };
  },
  props: {
    rules: {
      type: RegExp,
    },
    msg: {
      type: String,
      default: "数据不合法",
    },
  },
  methods: {
    handerInput(e) {
      this.proName = "class";
      //发出事件,发出文本框内容
      let v = e.target.value;
      if (this.rules) {
        if (this.rules.test(v)) {
          this.isOk = true;
        } else {
          this.isOk = false;
        }
      }
      this.$emit("input", v);
    },
    handerBlur(e) {
      let v = e.target.value;
      if (this.rules) {
        if (!this.rules.test(v)) this.$toast(this.msg);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.myinput {
  width: 318/360 * 100vw;
  height: 60px;
  outline: none;
  border: none;
  border-bottom: 3px solid #ccc;
  font-size: 20px;
  line-height: 60px;
}
.success {
  border-bottom-color: green;
}
.error {
  border-bottom-color: red;
}
</style>